MODULE ast
  
  USE definitions
  
  IMPLICIT NONE
  
CONTAINS
  
  SUBROUTINE ast_main
    
    USE definitions
    USE init
    USE inout
    USE erosion
    USE transport
    USE deposition
    USE avalanche_modules
    USE functions
    USE sources
    
    IMPLICIT NONE
    
    INTEGER :: bin = 0
    INTEGER :: k, i, j
    INTEGER, DIMENSION(2) :: pos
    REAL :: mass, d
    
    !------------------------------------------------------------
    
    CALL init_relpos
    CALL init_counter
    CALL init_output
    
    IF(qifconv==0) THEN
       DO i=0,steps+1,1
          CALL shuffle
          IF(source == 1) CALL wind_surce
          sourcebuf = 0
          DO j=1,xsize*ysize
             CALL output(i,j) 	!Print output to screen and write frames for certain steps
             CALL poll_cell(pos, j)
             CALL aetransport(pos)	!Erodes Slab from polled cell, moves it downwards and deposes it again
          ENDDO
       ENDDO
    ELSEIF(qifconv==1) THEN
       i = 0
       DO WHILE(qtrig == 0 .AND. i < steps + 1)
          i = i + 1
          CALL shuffle
          IF(source == 1) CALL wind_surce
          sourcebuf = 0
          DO j=1,xsize*ysize
             CALL output(i,j) 	!Print output to screen and write frames for certain steps
             CALL poll_cell(pos, j)
             CALL aetransport(pos)	!Erodes Slab from polled cell, moves it downwards and deposes it again
          ENDDO
       END DO
    ENDIF
    
    CALL final_output
    
  END SUBROUTINE ast_main
  
END MODULE ast
